﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using System.IO;
using KeanuMvc.Reference;
using HIS.Utils.Securities;
using HIS.Utils;
using HISModels.Entities;
using System.Configuration;
using Newtonsoft.Json.Linq;
using HISModels.ViewModel;

namespace HIS.Controllers.Master {
    public class GroupSpecialist : HIS.Controllers.AppBaseController {
        #region Fields
        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        private RefSpecialistGroup reffSpecialistGroup = null;
        private RefSpecialist reffSpecialist = null;
        private RefSubSpecialist reffSubSpecialist = null;
        #endregion

        #region Group Specialist
        public GroupSpecialist() {
            Data.Add("Pagetitle", "Group Specialist List");
            Data.Add("Notification", strNotification);
        }

        [Authorization(RoleAction = "master-specialist")]
        public override void Index() {
            try {
                Data["Pagetitle"] += " :: Group Specialist";
                RenderFile("master\\specialistgroup.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public void TableList() {
            try {

                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];

                string namevalue = HttpContext.Current.Request["namevalue"].NullSafeToString();
                string descvalue = HttpContext.Current.Request["descvalue"].NullSafeToString();

                string ssearch = "namevalue=" + namevalue + ";descvalue=" + descvalue;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                //pagingSearch.sSearch = HttpContext.Current.Request["sSearch"];
                if (!string.IsNullOrEmpty(ssearch)) {
                    pagingSearch.sSearch = ssearch;
                } else { pagingSearch.sSearch = Convert.ToString(HttpContext.Current.Request["sSearch"]); }
                pagingSearch.sSearch = ssearch;

                string strUri = string.Empty;
                strUri = Global.ServiceUriDictionary["groupspecpaging"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));


                strJson = stringReader.ReadToEnd();
                stringReader.Dispose();
                base.RestService.Dispose();


                HttpContext.Current.Response.Write(strJson);

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public void EditGroupSpecialist() {
            StringReader stringReader = null;
            /// Get id record from querystring
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            } catch {
                /// Throw to list if something goes wrong
                GoToUrl("groupspecialist/"); return;
            }

            try {
                ///Set Rest Url///
                string strUri = Global.ServiceUriDictionary["groupspec"] + id + Global.ServiceUriDictionary["requestformat"];
                ///Get Rest///
                stringReader = base.RestService.GetJson(strUri);
                ///Deserialize json response to object///
                reffSpecialistGroup = JsonConvert.DeserializeObject<RefSpecialistGroup>(stringReader.ReadToEnd());
                ///Set variable for rendering on view HTML///
                Data["Pagetitle"] += " :: Edit Group Specialist ::";
                Data.Add("reffSpecialistGroup", reffSpecialistGroup);
                Data.Add("IsCreate", "false");
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\editgroupspecialist.html");
                stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public void DeleteGroupSpecialist() {
            StringReader stringReader = null;
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            } catch {
                GoToUrl("groupspecialist/");
                return;
            }

            try {
                reffSpecialistGroup = new HISModels.Entities.RefSpecialistGroup();
                //string strUri = Data["servicerooturl"] + "sys/actiongroups/?format=json";
                string strUri = Global.ServiceUriDictionary["groupspec"] + Global.ServiceUriDictionary["requestformat"];

                stringReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));
                var a = JsonConvert.DeserializeObject<RefSpecialistGroup>(stringReader.ReadToEnd());
                if (strNotification == string.Empty) strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                Data["Notification"] = strNotification;

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                //RenderFile("Admins\\Actions\\editgroupspecialist.html");
                //stringReader.Dispose();
                //base.RestService.Dispose();


                HttpContext.Current.Response.Redirect(Data["rooturl"] + "master/groupspecialist/");

                //GoToUrl("groupspecialist/");

                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public void SaveGroupSpecialist() {
            StringReader stringReader = null;
            string strUri = string.Empty;
            try {
                reffSpecialistGroup = new RefSpecialistGroup(); Utils.Helper.SetNullToEmptyString(reffSpecialistGroup);
                if (HttpContext.Current.Request["IsCreate"] != "true") {
                    int id = 0;
                    try {
                        id = Convert.ToInt32(HttpContext.Current.Request["idem"]);
                    } catch { }
                    strUri = Global.ServiceUriDictionary["groupspec"] + id + Global.ServiceUriDictionary["requestformat"];
                    ///Get Rest///
                    stringReader = base.RestService.GetJson(strUri);
                    reffSpecialistGroup = JsonConvert.DeserializeObject<RefSpecialistGroup>(stringReader.ReadToEnd());
                }

                reffSpecialistGroup.PostHandler<HISModels.Entities.RefSpecialistGroup>();

                strUri = Global.ServiceUriDictionary["groupspec"] + Global.ServiceUriDictionary["requestformat"];

                if (HttpContext.Current.Request["IsCreate"] == "true") {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(reffSpecialistGroup));
                } else {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(reffSpecialistGroup));
                }
                string temp = stringReader.ReadToEnd();


                reffSpecialistGroup = JsonConvert.DeserializeObject<RefSpecialistGroup>(temp);

                if (reffSpecialistGroup != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }


                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";
                HttpContext.Current.Response.Write(strNotification);

            } catch (Exception ex) {
                //strNotification = "{\"strNotification\": \"" + ex.Message + "\"}";
                HttpContext.Current.Response.Write(ex.Message);
            } finally {

                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public void CreateSpecialistGroup() {

            try {
                Data["Pagetitle"] += " :: Add Group Specialist";
                reffSpecialistGroup = new HISModels.Entities.RefSpecialistGroup() { NameValue = string.Empty, DescValue = string.Empty };
                string GroupName = HttpContext.Current.Request["groupname"];
                Data.Add("reffSpecialistGroup", reffSpecialistGroup);
                Data.Add("IsCreate", "true");
                Data["Notification"] = strNotification;

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\editgroupspecialist.html");
                base.RestService.Dispose();
            }
        }

        #endregion

        #region Action Specialist

        [Authorization(RoleAction = "master-specialist")]
        public void SettingSpecialist() {
            //StringReader stringReader = null;
            int id = 0;
            int groupid = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
                groupid = Convert.ToInt32(HttpContext.Current.Request["groupid"]);
            } catch {
                GoToUrl("groupspecialist/");
                return;
            }
            try {

                string strUri = Global.ServiceUriDictionary["groupspec"] + groupid + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.GetJson(strUri);
                HISModels.Entities.RefSpecialistGroup reffSpecialistGroup = JsonConvert.DeserializeObject<RefSpecialistGroup>(stringReader.ReadToEnd());

                //Data
                Data["Pagetitle"] = "Specialty :: [Group Specialty (" + reffSpecialistGroup.NameValue + ") ]::";

                //string DdDropSpec = GetDropspec("DdDropspec", base.RestService, string.Empty, id.ToString(), string.Empty, false);
                //Data.Add("DdDropspec", DdDropppec);
                ///or
                Data.Add("DdDropspec", GetDropspec("DdDropspec", base.RestService, null).NullSafeToString());
                Data.Add("DdDropsubspec", GetDropsubspec("DdDropsubspec", base.RestService, null).NullSafeToString());
                Data.Add("Groupname", reffSpecialistGroup.NameValue.NullSafeToString());
                Data.Add("reffSpesialistGroup", reffSpecialistGroup);
                Data.Add("GroupSpecialistID", reffSpecialistGroup.Id.NullSafeToString());
                HttpContext.Current.Session["GroupSpecialistID"] = reffSpecialistGroup.Id.NullSafeToString();
                //RenderFile("Master\\specialist.html");
                RenderFile("Master\\specialist.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }
        //TABLELIST
        [Authorization(RoleAction = "master-specialist")]
        public void TableListSpecialist() {
            try {
                string id = HttpContext.Current.Session["GroupSpecialistID"].ToString();
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];
                string DdDropSpec = HttpContext.Current.Request["DdDropSpec"].NullSafeToString();
                string namevalue = HttpContext.Current.Request["namevalue"].NullSafeToString();
                string codevalue = HttpContext.Current.Request["codevalue"].NullSafeToString();
                string ssearch = "id=" + id + ";namevalue=" + namevalue + ";codevalue=" + codevalue;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                //pagingSearch.sSearch = HttpContext.Current.Request["sSearch"];
                pagingSearch.sSearch = ssearch;

                string strUri = "";
                strUri = Global.ServiceUriDictionary["specbygroup"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));


                strJson = stringReader.ReadToEnd();
                stringReader.Dispose();
                base.RestService.Dispose();


                HttpContext.Current.Response.Write(strJson);

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }


        [Authorization(RoleAction = "master-specialist")]
        public string DropDownSpecialist(string Selected) {
            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            //string strUri = Data["servicerooturl"] + "sys/actiongroups/?format=json";
            string strUri = Global.ServiceUriDictionary["groupspec"] + Global.ServiceUriDictionary["requestformat"];


            try {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<RefSpecialistGroup> listAction = JsonConvert.DeserializeObject<List<RefSpecialistGroup>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();
                activeArr.Add(new object[] { "Select" });
                foreach (RefSpecialistGroup item in listAction) activeArr.Add(new object[] { item.Id, item.Id + " - " + item.NameValue });

                strResult = KeanuMvc.Reference.Html.DropDown("Idd", string.Empty, activeArr, Selected);
                activeArr.Clear();
                activeArr = null;
            } catch {
            } finally {
                stringReaderDropDown.Dispose();
            }
            return strResult;
        }

        [Authorization(RoleAction = "master-specialist")]
        public void EditSpecialist() {
            StringReader stringReader = null;
            /// Get id record from querystring
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["Id"]);
            } catch {
                /// Throw to list if something goes wrong
                GoToUrl("groupspecialist/"); return;
            }

            try {

                ///Set Rest Url///
                string strUri = Global.ServiceUriDictionary["specialist"] + id + Global.ServiceUriDictionary["requestformat"];
                ///Get Rest///
                stringReader = base.RestService.GetJson(strUri);
                ///Deserialize json response to object///
                reffSpecialist = JsonConvert.DeserializeObject<RefSpecialist>(stringReader.ReadToEnd());
                ///Set variable for rendering on view HTML///
                Data["Pagetitle"] += " ::Edit Specialist";
                //Data.Add("ddactionspec", DropDownSpecialist(reffSpecialist.GroupSpecialistId.NullSafeToString()));
                Data.Add("reffSpecialist", reffSpecialist);
                //Data.Add("ddspecialist", DropDownSpecialist(reffSpecialist.GroupSpecialistId.NullSafeToString()));
                Data.Add("DdDropspec", GetDropspec("DdDropspec", base.RestService, "").NullSafeToString());
                Data.Add("IsCreate", "false");
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\editspecialist.html");
                stringReader.Dispose();
                base.RestService.Dispose();
            }
        }



        //============================================

        [Authorization(RoleAction = "master-specialist")]
        public void CreateSpecialist() {

            try {
                Data["Pagetitle"] += " :: Add Specialist ::";

                reffSpecialist = new HISModels.Entities.RefSpecialist() { NameValue = string.Empty, CodeValue = string.Empty };
                reffSpecialistGroup = new HISModels.Entities.RefSpecialistGroup() { NameValue = string.Empty, DescValue = string.Empty };
                //Data.Add("ddspecialist", DropDownSpecialist(reffSpecialist.GroupSpecialistId.NullSafeToString()));
                string groupid = HttpContext.Current.Request["groupid"];
                string GroupName=HttpContext.Current.Request["groupname"];
                Data.Add("ddspecialist", DropDownSpecialist(reffSpecialist.Id.NullSafeToString()));
                Data.Add("nnspecialist", reffSpecialist.GroupSpecialistId.NullSafeToString());
                //string DdHuLobRelId = GetHuLobRel("HuLobRelId", base.RestService, string.Empty, id.ToString(), "class=required", true);
                ////Data.Add("DdLobRel", DdHuLobRelId);
                //                //string DdLobRel = Convert.ToString(HttpContext.Current.Session["DdLobRel"]);
                //Data.Add("DdLobRel", DdHuLobRelId);
                //string DdDropSpec = GetDropSpec("HuLobRelId", base.RestService, string.Empty, id.ToString(), string.Empty, false);
                //Data.Add("DdDropSpec", DdDropSpec);
                ///or
                //Data.Add("DdDropspec", GetDropspec("DdDropspec", base.RestService, groupid).NullSafeToString());
                Data.Add("GroupName", GroupName);
                //sysAction.ActionGroupId = Convert.ToInt32(HttpContext.Current.Request["groupid"]);
                reffSpecialist.Id = Convert.ToInt32(HttpContext.Current.Request["groupid"]);
                //reffSpecialistGroup.NameValue = Convert.ToString(HttpContext.Current.Request["specialistlName"]);
                //Data.Add("specialistName", reffSpecialistGroup.NameValue);
                //Data.Add("Actionform", Data["rooturl"] + "/admins/actiongroup/saveaction/");
                Data.Add("reffSpecialist", reffSpecialist);
                Data.Add("IsCreate", "true");
                Data["Notification"] = strNotification;
                //Data.Add("GroupSpecialistID", reffSpecialist.GroupSpecialistId.NullSafeToString());
                //HttpContext.Current.Session["GroupSpecialistID"] = reffSpecialist.GroupSpecialistId.NullSafeToString();


            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\createspecialist.html");
                base.RestService.Dispose();
            }
        }


        #endregion

        [Authorization(RoleAction = "master-specialist")]
        public void SaveSpecialist() {
            StringReader stringReader = null;
            string strUri = string.Empty;
            try {
                reffSpecialist = new RefSpecialist(); Utils.Helper.SetNullToEmptyString(reffSpecialist);
                if (HttpContext.Current.Request["IsCreate"] != "true") {
                    int id = 0;
                    try {
                        id = Convert.ToInt32(HttpContext.Current.Request["idSpecialist"]);
                    } catch { }
                    strUri = Global.ServiceUriDictionary["specialist"] + id + Global.ServiceUriDictionary["requestformat"];
                    ///Get Rest///
                    stringReader = base.RestService.GetJson(strUri);
                    reffSpecialist = JsonConvert.DeserializeObject<RefSpecialist>(stringReader.ReadToEnd());
                }

                reffSpecialist.PostHandler<HISModels.Entities.RefSpecialist>();
                reffSpecialist.GroupSpecialistId = Convert.ToInt32(HttpContext.Current.Request["Idd"]);
                strUri = Global.ServiceUriDictionary["specialist"] + Global.ServiceUriDictionary["requestformat"];

                if (HttpContext.Current.Request["IsCreate"] == "true") {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(reffSpecialist));
                } else {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(reffSpecialist));

                }
                string temp = stringReader.ReadToEnd();


                reffSpecialist = JsonConvert.DeserializeObject<RefSpecialist>(temp);

                if (reffSpecialist != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }


                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";
                HttpContext.Current.Response.Write(strNotification);

            } catch (Exception ex) {
                //strNotification = "{\"strNotification\": \"" + ex.Message + "\"}";
                HttpContext.Current.Response.Write(ex.Message);
            } finally {

                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public void DeleteSpecialist() {
            StringReader stringReader = null;
            int id = 0;
            //int groupid = 0;
            try {
                //groupid = Convert.ToInt32(HttpContext.Current.Request["groupid"]);
                id = int.Parse(HttpContext.Current.Request["id"]);
            } catch {
                GoToUrl("groupspecialist/");
                return;
            }
            try {
                reffSpecialist = new HISModels.Entities.RefSpecialist();

                string strUri = Global.ServiceUriDictionary["specialist"] + Global.ServiceUriDictionary["requestformat"];

                StringReader strReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));
                var a = JsonConvert.DeserializeObject<RefSpecialist>(strReader.ReadToEnd());

                if (string.IsNullOrEmpty(strNotification)) strNotification = Helper.InformationMessage(strNotification);
                Data["Notification"] = strNotification;

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\specialist.html");
                //'/admins/actiongroup/editactionsetting/?id=" + o.aData['Id'] + "'
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public string GetDropspec(string name, KeanuMvc.Reference.ClientRestService clientService, string Selected) {
            string strResult = string.Empty;

            string strUri = Global.ServiceUriDictionary["groupspec"] + Global.ServiceUriDictionary["requestformat"];
            StringReader stringReader = clientService.GetJson(strUri);
            List<RefSpecialistGroup> listOption = JsonConvert.DeserializeObject<List<RefSpecialistGroup>>(stringReader.ReadToEnd());

            List<object[]> listDropdown = new List<object[]>();
            //listDropdown.Add(new object[] { "-1", "--Select--" });
            foreach (RefSpecialistGroup item in listOption) {
                listDropdown.Add(new object[] { item.Id, item.Id.ToString("00") + " - " + item.NameValue });
            }

            strResult = KeanuMvc.Reference.Html.DropDown("Idd", string.Empty, listDropdown, Selected);
            listDropdown.Clear();
            listDropdown = null;
            stringReader.Dispose();
            return strResult;
        }

        #region SubSpecialist
        [Authorization(RoleAction = "master-specialist")]
        public void SettingSubSpecialist() {
            //StringReader stringReader = null;
            //int id = 0;
            int groupid = 0;
            try {
                groupid = Convert.ToInt32(HttpContext.Current.Request["groupid"]);
            } catch {
                GoToUrl("groupspecialist/");
                return;
            }
            try {

                string strUri = Global.ServiceUriDictionary["specialist"] + groupid + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.GetJson(strUri);
                HISModels.Entities.RefSpecialist reffSpecialist = JsonConvert.DeserializeObject<RefSpecialist>(stringReader.ReadToEnd());


                Data["Pagetitle"] = "Sub Specialist :: [ Specialist (" + reffSpecialist.NameValue + ") ]::";
                Data.Add("reffSpesialist", reffSpecialist);
                Data.Add("Specialistname", reffSpecialist.NameValue.NullSafeToString());
                Data.Add("DdDropsubspec", GetDropsubspec("DdDropsubspec", base.RestService, null).NullSafeToString());
                //Data.Add("Groupname", reffSpecialistGroup.NameValue.NullSafeToString());
                Data.Add("SpecialistID", reffSpecialist.Id);
                Data.Add("SpecialistID2", reffSpecialist.GroupSpecialistId);
                HttpContext.Current.Session["SpecialistID"] = reffSpecialist.Id.NullSafeToString();
                HttpContext.Current.Session["SpecialistID2"] = reffSpecialist.GroupSpecialistId.NullSafeToString();
                RenderFile("Master\\subspecialist.html");


            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        //TABLELISTsUB
        [Authorization(RoleAction = "master-specialist")]
        public void TableListSpecialistsub() {

            try {
                string id = HttpContext.Current.Session["SpecialistID"].ToString();
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];

                string namevalue = HttpContext.Current.Request["namevalue"].NullSafeToString();
                string codevalue = HttpContext.Current.Request["codevalue"].NullSafeToString();

                string ssearch = "id=" + id + ";namevalue=" + namevalue + ";codevalue=" + codevalue;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                //pagingSearch.sSearch = HttpContext.Current.Request["sSearch"];
                if (!string.IsNullOrEmpty(ssearch)) {
                    pagingSearch.sSearch = ssearch;
                } else { pagingSearch.sSearch = Convert.ToString(HttpContext.Current.Request["sSearch"]); }
                pagingSearch.sSearch = ssearch;

                string strUri = string.Empty;
                strUri = Global.ServiceUriDictionary["subbyspec"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));


                strJson = stringReader.ReadToEnd();
                stringReader.Dispose();
                base.RestService.Dispose();


                HttpContext.Current.Response.Write(strJson);

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public void CreateSubSpecialist() {

            try {

                Data["Pagetitle"] += " :: Add Sub Specialist ::";
                reffSpecialist = new HISModels.Entities.RefSpecialist() { NameValue = string.Empty, CodeValue = string.Empty };
                reffSubSpecialist = new HISModels.Entities.RefSubSpecialist() { NameValue = string.Empty, CodeValue = string.Empty };
                string groupid = HttpContext.Current.Request["groupid"];
                string specialistname = HttpContext.Current.Request["specialistname"];

                Data.Add("ddsub", DropDownSub(reffSpecialist.GroupSpecialistId.NullSafeToString()));
                Data.Add("DdDropsubspec", GetDropsubspec("DdDropsubspec", base.RestService, groupid).NullSafeToString());
                reffSubSpecialist.Id = Convert.ToInt32(HttpContext.Current.Request["groupid"]);
                //reffSubSpecialist.NameValue = Convert.ToString(HttpContext.Current.Request["specialistname"]);
                Data.Add("groupid", groupid);
                Data.Add("reffSubSpecialist.SpecialistId", reffSubSpecialist.SpecialistId);
                Data.Add("reffSubSpecialist", reffSubSpecialist);
                Data.Add("IsCreate", "true");
                Data["Notification"] = strNotification;

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\createsubspecialist.html");
                base.RestService.Dispose();
            }
        }


        [Authorization(RoleAction = "master-specialist")]
        public void EditSpecialistsub() {
            StringReader stringReader = null;
            /// Get id record from querystring
            //int groupid = 0;
            int id = 0;
            try {
                //groupid = Convert.ToInt32(HttpContext.Current.Request["id"]);
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            } catch {
                /// Throw to list if something goes wrong
                GoToUrl("groupspecialist/"); return;
            }

            try {

                string strUri = Global.ServiceUriDictionary["subspec"] + id + Global.ServiceUriDictionary["requestformat"];

                stringReader = base.RestService.GetJson(strUri);
                Data["Pagetitle"] += " :: Edit Sub Specialist ::";
                reffSubSpecialist = JsonConvert.DeserializeObject<RefSubSpecialist>(stringReader.ReadToEnd());
                Data.Add("ddsub", DropDownSub(reffSubSpecialist.SpecialistId.NullSafeToString()));
                Data.Add("reffSubSpecialist", reffSubSpecialist);
                Data.Add("IsCreate", "false");
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\editsubspecialist.html");
                stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public void DeleteSubSpecialist() {
            StringReader stringReader = null;
            int id = 0;
            //int groupid = 0;
            try {
                //groupid = Convert.ToInt32(HttpContext.Current.Request["groupid"]);
                id = int.Parse(HttpContext.Current.Request["id"]);
            } catch {
                GoToUrl("groupspecialist/");
                return;
            }
            try {
                reffSubSpecialist = new HISModels.Entities.RefSubSpecialist();

                string strUri = Global.ServiceUriDictionary["subspec"] + Global.ServiceUriDictionary["requestformat"];

                StringReader strReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));
                var a = JsonConvert.DeserializeObject<RefSubSpecialist>(strReader.ReadToEnd());

                if (string.IsNullOrEmpty(strNotification)) strNotification = Helper.InformationMessage(strNotification);
                Data["Notification"] = strNotification;

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("Master\\subspecialist.html");
                //'/admins/actiongroup/editactionsetting/?id=" + o.aData['Id'] + "'
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-specialist")]
        public string DropDownSub(string Selected) {
            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            //string strUri = Data["servicerooturl"] + "sys/actiongroups/?format=json";
            string strUri = Global.ServiceUriDictionary["specialist"] + Global.ServiceUriDictionary["requestformat"];


            try {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<RefSpecialist> listAction = JsonConvert.DeserializeObject<List<RefSpecialist>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();

                foreach (RefSpecialist item in listAction) activeArr.Add(new object[] { item.Id, item.CodeValue + " - " + item.NameValue });

                strResult = KeanuMvc.Reference.Html.DropDown("Idd", string.Empty, activeArr, Selected);
                activeArr.Clear();
                activeArr = null;
            } catch {
            } finally {
                stringReaderDropDown.Dispose();
            }
            return strResult;
        }

        [Authorization(RoleAction = "master-specialist")]
        public void SaveSubSpecialist() {
            StringReader stringReader = null;
            string strUri = string.Empty;
            try {
                reffSubSpecialist = new RefSubSpecialist(); Utils.Helper.SetNullToEmptyString(reffSubSpecialist);
                if (HttpContext.Current.Request["IsCreate"] != "true") {
                    int id = 0;
                    try {
                        id = Convert.ToInt32(HttpContext.Current.Request["idsub"]);
                    } catch { }
                    strUri = Global.ServiceUriDictionary["subspec"] + id + Global.ServiceUriDictionary["requestformat"];
                    ///Get Rest///
                    stringReader = base.RestService.GetJson(strUri);
                    reffSubSpecialist = JsonConvert.DeserializeObject<RefSubSpecialist>(stringReader.ReadToEnd());
                }

                reffSubSpecialist.PostHandler<HISModels.Entities.RefSubSpecialist>();
                reffSubSpecialist.SpecialistId = Convert.ToInt32(HttpContext.Current.Request["Idd"]);
                strUri = Global.ServiceUriDictionary["subspec"] + Global.ServiceUriDictionary["requestformat"];

                if (HttpContext.Current.Request["IsCreate"] == "true") {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(reffSubSpecialist));
                } else {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(reffSubSpecialist));

                }
                string temp = stringReader.ReadToEnd();


                reffSubSpecialist = JsonConvert.DeserializeObject<RefSubSpecialist>(temp);

                if (reffSubSpecialist != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }


                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";
                HttpContext.Current.Response.Write(strNotification);

            } catch (Exception ex) {
                //strNotification = "{\"strNotification\": \"" + ex.Message + "\"}";
                HttpContext.Current.Response.Write(ex.Message);
            } finally {

                base.RestService.Dispose();
            }
        }


        [Authorization(RoleAction = "master-specialist")]
        public string GetDropsubspec(string name, KeanuMvc.Reference.ClientRestService clientService, string Selected) {
            string strResult = string.Empty;

            string strUri = Global.ServiceUriDictionary["specialist"] + Global.ServiceUriDictionary["requestformat"];
            StringReader stringReader = clientService.GetJson(strUri);
            List<RefSpecialist> listOption = JsonConvert.DeserializeObject<List<RefSpecialist>>(stringReader.ReadToEnd());

            List<object[]> listDropdown = new List<object[]>();
            listDropdown.Add(new object[] { "-1", "--Select--" });
            foreach (RefSpecialist item in listOption) {
                listDropdown.Add(new object[] { item.Id, item.NameValue });
            }
            strResult = KeanuMvc.Reference.Html.DropDown("Idd", string.Empty, listDropdown, Selected);
            //strResult = KeanuMvc.Reference.Html.DropDown(name, string.Empty, listDropdown, Selected);
            listDropdown.Clear();
            listDropdown = null;
            stringReader.Dispose();
            return strResult;
        }

        #endregion
    }

}

        





